import {AsyncStorage} from 'react-native'
import Request from "../utils/Request";
import {ProductService} from "./services/ProductService";
import constant from "../utils/constant";
import RouterServer from "../utils/RouterServer";
import {AuthModel, ProductModel} from "../interface/index";

const {Toast} = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_product";
export default {
    namespace: 'product',
    state: {
        loading: false,
        categoryList: [],
        topCategoryList: [],
        productList: [],
        product_list_next_page_url: null,

        selectedCategoryIndex: 0
    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload}
        }
    },
    effects: {
        //保存到本地
        * saveToStorage({payload}, {call}) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload))
        },

        //本地恢复
        * restoreFromStorage({payload}, {call, put, select}) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: {...JSON.parse(data)}
            });
        },

        //获取分类列表
        * fetchCategoryList({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {loading: true},
            });
            const auth: AuthModel = yield select(state => state.auth);
            const data = yield call(ProductService.getCategories, {store_id: auth.profile.id});
            yield put({
                type: 'save',
                payload: {loading: false},
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {categoryList: data.data},
                });
                const product:ProductModel = yield select(state=>state.product);
                if(product.categoryList.length>0&&product.categoryList[product.selectedCategoryIndex]){
                    yield put({
                        type:'fetchProductList',
                        payload:{store_cate_id:product.categoryList[product.selectedCategoryIndex].id}
                    })
                }

            }
        },

        //获取商品列表
        * fetchProductList({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {loading: true},
            });
            const auth: AuthModel = yield select(state => state.auth);
            const data = yield call(ProductService.list, {store_id: auth.profile.id, ...payload});
            yield put({
                type: 'save',
                payload: {loading: false},
            });
            if (data) {
                const product_list_next_page_url = data.data.next_page_url ? data.data.next_page_url : null;
                yield put({
                    type: 'save',
                    payload: {productList: data.data.data, product_list_next_page_url},
                });
            }
        },
        //获取商品列表
        * fetchMoreProductList({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {loading: true},
            });
            const auth: AuthModel = yield select(state => state.auth);
            const product: ProductModel = yield select(state => state.product);
            if (product.product_list_next_page_url) {
                const data = yield call(ProductService.more, product.product_list_next_page_url, {store_id: auth.profile.id, ...payload});
                yield put({
                    type: 'save',
                    payload: {loading: false},
                });
                if (data) {
                    const product_list_next_page_url = data.data.next_page_url ? data.data.next_page_url : null;
                    let productList = Object.assign([], product.productList);
                    if (data.data.data) {
                        productList = productList.concat(data.data.data)
                    }
                    yield put({
                        type: 'save',
                        payload: {productList, product_list_next_page_url},
                    });
                }
            }
            yield put({
                type: 'save',
                payload: {loading: false},
            });

        },

        //创建自定义分类
        * createCategory({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: {loading: true}
            });
            const data = yield call(ProductService.createCategory, {store_id: auth.profile.id, ...payload});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                Toast.success('自定义分类创建成功', constant.ToastDuration);
                yield put({
                    type: 'fetchCategoryList'
                });
                yield call(RouterServer.pop)
            }
        },

        //获取顶级分类
        * fetchTopCategories({payload}, {call, put, select}) {
            const product: ProductModel = yield select(state => state.product);
            if (product.topCategoryList.length <= 0) {
                const data = yield call(ProductService.topCategories);

                if (data) {

                    yield put({
                        type: 'save',
                        payload: {topCategoryList: data.data}
                    })
                }
            }
        },
        //添加商品
        * add({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {loading: true}
            });
            const auth: AuthModel = yield select(state => state.auth);
            const data = yield call(ProductService.add, {...payload, store_id: auth.profile.id});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                Toast.success('商品添加成功', constant.ToastDuration);
                yield call(RouterServer.pop);
            }
        },
        //修改商品
        * edit({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {loading: true}
            });
            const auth: AuthModel = yield select(state => state.auth);
            const data = yield call(ProductService.edit, {...payload, store_id: auth.profile.id});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                Toast.success('商品修改成功', constant.ToastDuration);
            }
        },
        //删除商品
        * del({payload}, {call, put, select}) {
            yield put({
                type: 'save',
                payload: {loading: true}
            });
            const auth: AuthModel = yield select(state => state.auth);
            const data = yield call(ProductService.del, {...payload, store_id: auth.profile.id});
            yield put({
                type: 'save',
                payload: {loading: false}
            });
            if (data) {
                Toast.success('商品删除成功', constant.ToastDuration);
                yield call(RouterServer.pop);
                yield call(RouterServer.refresh);
            }
        },


        * changeSelectedCategoryIndex({payload}, {put, select}) {
            let product: ProductModel = yield select(state => state.product);
            const selectedCategoryIndex = payload.index;
            if (product.categoryList[selectedCategoryIndex]) {
                yield put({
                    type: 'save',
                    payload: {
                        selectedCategoryIndex
                    }
                });
                yield put({
                    type: 'fetchProductList',
                    payload: {
                        store_cate_id: product.categoryList[selectedCategoryIndex].id
                    }
                })
            }
        },

        * clear({payload}, {put}) {
            yield put({
                type: 'save',
                payload: {productList: []}
            })
        }
    },
    subscriptions: {},
};
